package com.example.pandaso.engines.extend;

import java.util.ArrayList;
import java.util.List;

import com.example.pandaso.engines.BaseEngine;
import com.example.pandaso.pojo.BingSearchResult;
import com.example.pandaso.utils.HttpUtils;

public class BingEngine extends BaseEngine {

    @Override
    public List<String> getTips(String keyword) {
        List<String> suggestsList = new ArrayList<String>();
        // 调用Bing API获取智能提
        BingSearchResult bingSearchResult = HttpUtils.getObject("http://api.bing.com/qsonhs.aspx?type=cb&q=" + keyword,
                BingSearchResult.class);
        // 处理返回结果
        if (bingSearchResult.getAs().getResults() == null) {
                return suggestsList;
        } else {
            BingSearchResult.AS as = bingSearchResult.getAs();
            List<BingSearchResult.Result> results = as.getResults();

            for (BingSearchResult.Result r : results) {
                List<BingSearchResult.Suggest> suggests = r.getSuggests();

                for (BingSearchResult.Suggest s : suggests) {
                    String txt = s.getTxt();
                    suggestsList.add(txt);
                }
            }
            return suggestsList;
        }
    }
}